class TodoController {
    constructor(model, view) {
        this.model = model;
        this.view = view;

        this.view.rootElement.querySelector('form').addEventListener('submit', (event) => {
            event.preventDefault();
            this.addTodo(event.target.elements.todoInput.value);
            event.target.reset();
        });

        this.view.todoListElement.addEventListener('click', (event) => {
            if (event.target.tagName === 'LI') {
                this.toggleTodo(event.target.dataset.id);
            }
        });

        this.view.todoListElement.addEventListener('dblclick', (event) => {
            if (event.target.tagName === 'LI') {
                this.deleteTodo(event.target.dataset.id);
            }
        });

        this.renderTodos();
    }

    addTodo(text) {
        this.model.addTodo(text);
        this.renderTodos();
    }

    deleteTodo(id) {
        this.model.deleteTodo(id);
        this.renderTodos();
    }

    toggleTodo(id) {
        this.model.toggleTodo(id);
        this.renderTodos();
    }

    renderTodos() {
        const todos = this.model.getTodos();
        this.view.render(todos);
    }
}